<?php
include "../../../lib/list.php";
if($_POST['mode']=='insert'){
	$no						=	$_POST['no'];
	$productname			=	$_POST['name'];
	$productnamegana		=	$_POST['productnamegana'];
	$price1					=	$_POST['price1'];
	$price2					=	$_POST['price2'];
	$price3					=	$_POST['price3'];
	$unit					=	$_POST['unit'];
	$buyer					=	$_POST['buyer'];
	$buyingprice			=	$_POST['buyingprice'];
	$bumun_no				=	$_POST['bumun_no'];
	$b_no					=	$_POST['b_no'];
	$m_no					=	$_POST['m_no'];
	$s_no					=	$_POST['s_no'];
	$rot					=	$_POST['rot'];
	$rotcnt					=	$_POST['rotcnt'];
	$rot_barcode			=	$_POST['rot_barcode'];
	$pos					=	$_POST['pos'];
	$stock					=	$_POST['stock'];
	$minus					=	$_POST['minus'];
	$note1					=	$_POST['note1'];
	$note2					=	$_POST['note2'];
	$note3					=	$_POST['note3'];
	$productimagedir		=	$_FILES['productimagedir']['tmp_name'];
	$productimagedir_type	=	$_FILES['productimagedir']['type'];
	$productimagedir_name	=	$_FILES['productimagedir']['name'];
	$productimagedir_size	=	$_FILES['productimagedir']['size'];

	
	for($i=0;$i<count($no);$i++){
		
	
	if(!$productname[$i]){
		echo("
	    <script>
	      window.alert('제품명을 입력해주세요.');
	      window.alert('내용 입력에 실패했습니다.');
		  location.href='view.php';
	      </script>");
		return;
	}
	if(!$barcode[$i]){
		echo("
	    <script>
	      window.alert('바코드를 입력해주세요.');
	      window.alert('내용 입력에 실패했습니다.');
		  location.href='view.php';
	      </script>");
		return;
	}
	if(!$price1[$i]){
		echo("
	    <script>
	      window.alert('가격을 입력해주세요.');
	      window.alert('내용 입력에 실패했습니다.');
		  location.href='view.php';
	      </script>");
		return;
	}

	else{
		if($rot[$i]){
			if(!$rotcnt[$i]){
				echo("
			    <script>
			      window.alert('로트를 선택하셨다면 로트수는 필수 입니다.');
			      window.alert('내용 입력에 실패했습니다.');
				  location.href='view.php';
			      </script>");
				return;
			}
			if(!$rot_barcode[$i]){
				echo("
			    <script>
			      window.alert('로트를 선택하셨다면 로트바코드는 필수입니다.');
			      window.alert('내용 입력에 실패했습니다.');
				  location.href='view.php';
			      </script>");
				return;
			}
		}else{
			$rotcnt[$i] = "";
			$rot_barcode[$i] = "";
		}
		
		if(!$no[$i]){
			$no[$i] = 'NULL';
			$img_db = "";
		}
				
		if($productimagedir[$i]){
			$qry = "SELECT * FROM uatp_product WHERE no = $no[$i]";
			$product = DBarray($qry);
			$db_img = $product['productimagedir'];
		$productimagedir[$i] = up_img($productimagedir[$i],$productimagedir_type[$i],$productimagedir_size[$i], $productimagedir_name[$i],$db_img,"img",$dir);
		}else{
			$qry = "SELECT * FROM uatp_product WHERE no = $no[$i]";
			$product = DBarray($qry);
			$productimagedir[$i] = $product['productimagedir'];
		}
		
		$query = sprintf("INSERT INTO uatp_product
		(no, barcode, 
		productname, productnamegana, 
		productimagedir, price1, price2, 
		price3, unit, buyer, 
		buyingprice, bumun_no, 
		b_no, m_no, s_no, 
		rot, rotcnt, rot_barcode, 
		pos, stock, minus, note1, note2, note3)
		
		VALUES(%d,'%s','%s','%s','%s',
			   %d,%d,%d,'%s',%d,
			   %d,%d,%d,%d,%d,
			   '%s',%d,'%s','%s','%s',
			   '%s','%s','%s','%s')
		
		ON DUPLICATE KEY UPDATE 
		
		barcode='%s', productname='%s', productnamegana='%s', 
		productimagedir='%s', price1=%d, price2=%d, price3=%d, 
		unit='%s', buyer=%d, buyingprice=%d, bumun_no=%d, 
		b_no=%d, m_no=%d, s_no=%d, rot='%s', rotcnt=%d, 
		rot_barcode='%s', pos='%s', stock='%s', minus='%s',
		note1='%s', note2='%s', note3='%s'",
		
		$no[$i],	clean(strip_script($barcode[$i])),
		clean(strip_script($productname[$i])),
		clean(strip_script($productnamegana[$i])),
		clean(strip_script($productimagedir[$i])),
		$price1[$i],		$price2[$i],		$price3[$i],	
		clean(strip_script($unit[$i])),		$buyer[$i],		
		$buyingprice[$i],	 $bumun_no[$i],		$b_no[$i],	$m_no[$i], $s_no[$i],
		clean(strip_script($rot[$i])),		$rotcnt[$i],
		clean(strip_script($rot_barcode[$i])),
		clean(strip_script($pos[$i])), 		clean(strip_script($stock[$i])),
		clean(strip_script($minus[$i])),	clean(strip_script($note1[$i])),
		clean(strip_script($note2[$i])),	clean(strip_script($note3[$i])),
		
		clean(strip_script($barcode[$i])),
		clean(strip_script($productname[$i])),
		clean(strip_script($productnamegana[$i])),
		clean(strip_script($productimagedir[$i])),
		$price1[$i],		$price2[$i],		$price3[$i],	
		clean(strip_script($unit[$i])),		$buyer[$i],		
		$buyingprice[$i],	 $bumun_no[$i],		$b_no[$i],	$m_no[$i], $s_no[$i],
		clean(strip_script($rot[$i])),		$rotcnt[$i],
		clean(strip_script($rot_barcode[$i])),
		clean(strip_script($pos[$i])), 		clean(strip_script($stock[$i])),
		clean(strip_script($minus[$i])),	clean(strip_script($note1[$i])),
		clean(strip_script($note2[$i])),	clean(strip_script($note3[$i])));
		DBquery($query);
	}
	}
	mysql_close();
	echo "<script>
				window.alert('상품을 입력했습니다.');
				location.href='view.php';
		  </script>";
}
if($_POST['mode']=='delete'){
	$no=$_POST['chk_no'];
	for($i=0;$i<count($no);$i++){
		$qry = "SELECT * FROM uatp_product WHERE no = $no[$i]";
		$product = DBarray($qry);
		$db_img = $product['productimagedir'];
				@unlink($dir.$db_img);
				
		$query = sprintf("DELETE FROM `uatp_product` WHERE `no` = %d",$no[$i]);
		DBquery($query);
	}
	mysql_close();
	echo "<script>
							window.alert('삭제하였습니다.');
							location.href='view.php';
					  </script>";
}